import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.web.WebView;

public class DRFJW_frame {
    public GraphicsConfiguration gc=null;
    private JFXPanel jfxPanel;
    public static String URL = "http://localhost:8080/";
    // public static final String URL = "https://www.baidu.com/";

    public DRFJW_frame(String URI){
        URL=URI;
        // // 获取图形环境
        // GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        // // 获取所有显示设备
        // GraphicsDevice[] gds = ge.getScreenDevices();
        // if(gds.length > 1){
        //     // 获取第二个显示器的设备
        //     GraphicsDevice gd = gds[1];
        //     this.gc = gd.getDefaultConfiguration();
        // }else{
        //     GraphicsDevice gd = gds[0];
        //     this.gc = gd.getDefaultConfiguration();
        //     System.out.println("没有找到第二个显示器");
        // }
    }

    public void createAndShowWindow() {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] gds = ge.getScreenDevices();
        // for (GraphicsDevice gd : ge.getScreenDevices()) {
        for (int i=0;i<gds.length;i++) {
            JFrame frame;
            if(i==1){
                frame = new JFrame("QR Code");
                JButton quit = new JButton("Quit");
                quit.addActionListener(event -> System.exit(0));
                jfxPanel = new JFXPanel();
                Platform.runLater(this::createJFXContent);
                JPanel buttonPanel = new JPanel();
                buttonPanel.add(quit);
                frame.add(BorderLayout.CENTER, jfxPanel);
                frame.add(BorderLayout.SOUTH, buttonPanel);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(800, 800);
                // frame.setLocationRelativeTo(null);
                // frame.setVisible(true);
                frame.setLocation(gds[i].getDefaultConfiguration().getBounds().getLocation());
                frame.setUndecorated(true);
                frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
                frame.setVisible(true);
                gds[i].setFullScreenWindow(frame);
            }else{
                // 设定窗口标题
                frame = new JFrame("Window["+(i+1)+"]");
                // 添加窗口标识（标识屏幕）
                frame.add(createLabel(String.valueOf(i+1)));
                // 添加退出按钮
                JButton quit = new JButton("Quit");
                // 绑定退出事件
                quit.addActionListener(event -> System.exit(0));
                JPanel buttonPanel = new JPanel();
                buttonPanel.add(quit);
                frame.add(BorderLayout.SOUTH, buttonPanel);
                // 设定退出事件
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                // 设定窗口大小
                frame.setSize(500, 500);
                // 显示窗口
                frame.setVisible(true);
                // 最大化窗口
                // frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
            }
        }
    }

    private void createJFXContent() {
        WebView webView = new WebView();
        webView.getEngine().load(URL);
        Scene scene = new Scene(webView);
        jfxPanel.setScene(scene);
    }

    private JLabel createLabel(String label) {
        JLabel jLabel = new JLabel(label);
        jLabel.setHorizontalAlignment(JLabel.CENTER);
        jLabel.setFont(jLabel.getFont().deriveFont(200.0f));
        jLabel.setFocusable(true);
        return jLabel;
    }

    public static void main(String[] args) {
        DRFJW_frame swingApp = new DRFJW_frame("https://www.baidu.com/");
        SwingUtilities.invokeLater(swingApp::createAndShowWindow);
    }
}